  // pages/personalDetails/personalDetails.js
  Page({

    /**
     * 页面的初始数据
     */
    data: {
      cer_front_img: "", // 身份证正面
      cer_reverse_img: "", // 身份证反面
      cer_face: "", //正面半身照
      name: "", // 姓名
      cer_code: "", // 身份证号码
      bank_code: "", // 银行卡
      mobile: "", // 手机号码       
      v_code: "", // 验证码
      show: false, // 展示确认框
      show_code: false,
      code_title: "", // 倒计时-时间
      time: 120,
      isLoading: false,
      address: '',
      QRCodeType: '',
      type: '',
      showCam: false,
      enterprise_professional_facilitator_id: ''
    },
    addImage(e) {
      let {
        type
      } = e.currentTarget.dataset;
      let that = this;
      wx.showActionSheet({
        itemList: ['相册', '拍照'],
        success(res) {
          console.log(res.tapIndex)
          if (res.tapIndex == 0) {
            that.identity(type)
          }
          if (res.tapIndex == 1) {
            that.setData({
              showCam: true,
              type: type
            })
          }
        },
        fail(res) {
          console.log(res.errMsg)
        }
      })
    },
    cameraSuccess(res) {
      let {
        detail
      } = res;
      let that = this;
      console.log(res)
      that.setData({
        showCam: false
      })
      that.upimg(detail, that.data.type, that)
    },
    // 身份证
    identity(type) {
      //  type  1 身份证正面   2  反面 3半身照
      let self = this;
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album'],
        success(res) {
          const tempFilePaths = res.tempFilePaths;
          console.log(tempFilePaths, '返回参数图片')
          self.upimg(tempFilePaths[0], type, self)
        }
      })
    },
    _inputs: function (e) {
      let {
        types
      } = e.currentTarget.dataset || {}, value = e.detail.value || '';
      if (types) {
        let data = {};
        data[types] = value;
        this.setData(data)
      }
    },
    cancel: function () {
      this.setData({
        show: false
      })
    },
    // 开始签约
    start_sign: async function () {
      // this.setData({ show:true})
      let show_title = (msg) => {
        wx.$Toast(msg || '不能为空');
      }
      if (this.data.cer_front_img == '') {
        show_title('请上传身份证正面');
        return;
      }
      if (this.data.cer_reverse_img == '') {
        show_title('请上传身份证反面');
        return;
      }
      if (this.data.name == '') {
        show_title('姓名不能为空');
        return;
      }
      if (this.data.cer_code == '') {
        show_title('身份证号不能为空');
        return;
      }
      if (!wx.$isCard(this.data.cer_code)) {
        show_title('身份证号不正确');
        return;
      }
      if (this.data.mobile == '') {
        show_title('手机号不能为空');
        return;
      }
      if (!wx.$isMobile(this.data.mobile)) {
        show_title('手机号格式不正确');
        return;
      }
      if (this.data.v_code == '') {
        show_title('验证码不能为空');
        return;
      }
      if (this.data.QRCodeType == 'naturalPerson' && this.data.address == '') {
        show_title('地址不能为空');
        return;
      }
      this.setData({
        show: true
      })
    },
    async save() {
      if (this.data.isLoading) {
        console.log('::::::::::::::::::::::::loading')
        return;
      }
      this.setData({
        isLoading: true
      })
      let invitationCode = wx.getStorageSync('invitationCode')
      let data = {
        cer_front_img: this.data.cer_front_img,
        cer_reverse_img: this.data.cer_reverse_img,
        name: this.data.name,
        cer_code: this.data.cer_code,
        bank_code: this.data.bank_code.replace(/\s*/g, ""),
        mobile: this.data.mobile,
        code_type: 'xcx_add_userinfo',
        v_code: this.data.v_code
      }
      if (this.data.QRCodeType == 'naturalPerson') {
        data['address'] = this.data.address;
        data['invite_code'] = invitationCode;
        data['cer_face'] = this.data.cer_face;
      }
      wx.showLoading()
      wx.$ajax('/Api/saveProfessionalInfo', data, 'POST').then(res => {
        console.log(res, ':::::::::::::::::::res')
        wx.hideLoading()
        if (res.data.code == 200) {
          wx.setStorageSync('is_auth', res.data.data.is_auth)
          wx.$Toast('认证成功');
          if(res.data.data.token){
            wx.setStorageSync('token', res.data.data.token);
          }
          if (this.data.QRCodeType == 'naturalPerson') {
            wx.redirectTo({
              url: '/pages/business/business?&id=' + this.data.enterprise_professional_facilitator_id,
            })
          } else {
            if(res.data.data.is_contract == 1){
              wx.reLaunch({
                url: '/pages/index/index',
              })
            }else{
              if (wx.getStorageSync('invitationCode')) {
                this.sign(wx.getStorageSync('invitationCode'))
              } else {
                wx.redirectTo({
                  url: '/pages/newSigned/newSigned',
                })
              }
            }
         
          }

        } else {
          wx.$Toast(res.data.msg);
          this.setData({
            show: false,
            isLoading: false
          })
        }
        console.log(res, '新增返回')
      })
    },
    sign(codes) {
      wx.$api.contractEnterprise({
        invite_code: codes
      }).then(res => {
        if (res.data.code == '200') {
          let {
            is_auth,
            enterprise_id,
            is_contract,
            enterprise_professional_facilitator_id
          } = res.data.data || {};
          wx.setStorageSync('is_auth', is_auth);
          wx.setStorageSync('is_contract', is_contract);
          this.setData({
            enterprise_professional_facilitator_id: enterprise_professional_facilitator_id
          })
          if (is_auth == 1) {
            // wx.switchTab({
            //   url: '/pages/index/index',
            // })
            wx.reLaunch({
              url: '/pages/business/business?id=' + enterprise_professional_facilitator_id,
            })
          } else {
            wx.$Toast(res.data.msg);
          }
        } else {
          wx.$Toast(res.data.msg);
        }

      })
    },
    // 点击模态框
    onClickHide: function () {

    },
    noop: function () {},
    // 倒计时处理
    setTimeout() {
      let time = this.data.time || 120,
        that = this;
      let code_time = time;
      that.setData({
        code_title: code_time + 's',
        show_code: true
      })
      let T = setInterval(() => {
        code_time -= 1;
        if (code_time <= 0) {
          code_time = null;
          that.setData({
            code_title: '',
            show_code: false
          })
          clearInterval(T);
        } else {
          that.setData({
            code_title: code_time + 's',
            show_code: true
          })
        }
      }, 1000)
    },
    async getCode() {
      let self = this;
      if (self.data.mobile && wx.$isMobile(self.data.mobile)) {
        if (self.data.code_title) {
          return
        }
        this.setTimeout();
        let data = await wx.$api.sendCodes({
          mobile: self.data.mobile,
          code_type: 'xcx_add_userinfo'
        });
        // if(data.data.code==200){
        //   wx.$Toast('验证码发送成功');
        // }
      } else {
        wx.$Toast('请输入确定手机号码');
      }
    },
    upimg(i, type, self) {
      wx.showLoading({
        title: '图片上传中...',
        mask:true,
      })
      wx.uploadFile({
        url: wx.$config.uploadFile,
        filePath: i,
        name: 'file',
        success: res => {
          let data = typeof res.data == "string" ? JSON.parse(res.data) : null;
          console.log(res)
          if (type == 1) {
            // wx.getImageInfo({
            //   src: data.data.url,
            //   success: function (res) {
            //     if (res.height>res.width) {
            //       this.setData({
            //         rotate:true
            //       })
            //     }
            //   }
            // })
            self.setData({
              cer_front_img: data.data.url || ''
            })
            // wx.showLoading()
            wx.$api.idcardOcr({
              cer_front_img: data.data.url
            }).then(res => {
              wx.hideLoading()
              if (res.data.code == 200) {
                this.setData({
                  name: res.data.data.name || '',
                  cer_code: res.data.data.num || ''
                })
              }else{
                this.setData({
                  name: '',
                  cer_code: ''
                })
                wx.$Toast(res.data.msg);
              }
            })
          } else if (type == 2) {
            // wx.getImageInfo({
            //   src: data.data.url,
            //   success: function (res) {
            //     console.log(res);
            //     if (res.height>res.width) {
            //       this.setData({
            //         rotate:true
            //       })
            //     }
            //   }
            // })
            self.setData({
              cer_reverse_img: data.data.url || ''
            })
          } else if (type == 3) {
            self.setData({
              cer_face: data.data.url || ''
            })
          }
        },
        complete:()=>{
          wx.hideLoading()
        }
      })
    },

    // 查看协议
    look_agreement() {
      wx.navigateTo({
        url: '/pages/protocol/protocol',
      })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      console.log(wx.getStorageSync('invitationCode'))
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      let QRCodeType = wx.getStorageSync('QRCodeType');
      console.log(QRCodeType)

      this.setData({
        QRCodeType: QRCodeType
      })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    }
  })